package com.linkage.user.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.linkage.user.interceptor.NeedLogin;
import com.linkage.user.pojo.User;
import com.linkage.user.service.LoginService;
import com.linkage.user.service.UserService;
import com.linkage.verifycode.VerifyCode;

/**
 * @author WangY
 * 
 */
/**
 * @author WangY
 *
 */
@Controller
@RequestMapping("/")
public class LoginController {
	@Autowired
	private LoginService loginService;
	@Autowired
	private UserService userService;

	/**
	 * 首页
	 * 
	 * @return
	 */
	@NeedLogin
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String index() {
		return "index";
	}

	/**
	 * 进入登录页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/gotoLogin", method = RequestMethod.GET)
	public String gotoLogin() {
		return "login/login_form";
	}
	
	@RequestMapping(value = "/firstLogin", method = RequestMethod.GET)
	public String firstLogin() {
		return "login/firstlogin";
	}
	
	/**
	 * 登录操作
	 * 
	 * @return
	 */
	@RequestMapping(value = "/doLogin", method = RequestMethod.POST)
	public String login(@RequestParam("userName") String userName,
			@RequestParam("password") String password,
			HttpServletRequest request, Model model) {
		boolean login = this.loginService.login(request, userName, password);
		if (login) {
			User user = this.userService.loadByLoginName(userName);
			// 保存用户到session
			request.getSession().setAttribute("LOGIN_USER_KEY_IN_SESSION", user);
			return "menu/main";
		} else {
			model.addAttribute("errorMsg", "登录失败！");
			return "login/login_form";
		}
	}

	@RequestMapping(value = "/success", method = RequestMethod.GET)
	@NeedLogin
	public String success() {
		return "success";
	}
	/**
	 * 注册 页面
	 * @return
	 */
	
	/**
	 * 注册操作
	 * wangyong
	 * @return
	 */
	@RequestMapping(value = "/registerjsp", method = RequestMethod.GET)
	public String registerSuccess(HttpServletRequest request) {
		return "login/register";
	}
	
	@RequestMapping(value = "/register", method = RequestMethod.POST)
	public String register(User user, HttpServletRequest request) {

		if (this.loginService.checkRegister(user.getUserName(),
				user.getPassword(), user, request)) {
			this.loginService.register(user, request);

			return "redirect:/gotoLogin";
		} else {
			return "redirect:/registerjsp";
		}
		
	}
    
	// /bookshop home page
	@RequestMapping(value = "/main", method = RequestMethod.GET)
	public String main() {
		return "menu/main";
		// https://git.oschina.net/liangyue/OnlineBookShop.git;
	}

	@RequestMapping(value = "/top", method = RequestMethod.GET)
	public String top() {
		return "menu/top";
	}

	@RequestMapping(value = "/left", method = RequestMethod.GET)
	public String left() {
		return "menu/left";
	}

	@RequestMapping(value = "/body1", method = RequestMethod.GET)
	public String body() {
		return "menu/body1";
	}
	
	@RequestMapping(value = "/search", method = RequestMethod.GET)
	public String search() {
		return "menu/search";
	}
	
	@RequestMapping(value = "/gj", method = RequestMethod.GET)
	public String gj() {
		return "menu/gj";
	}

	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	@NeedLogin
	public String logout(HttpServletRequest request) {
		this.loginService.logout(request);
		return "redirect:/gotoLogin";
	}
}
